package com.sentinel.demo.pull.file.init;

import com.alibaba.csp.sentinel.datasource.AutoRefreshDataSource;
import com.alibaba.csp.sentinel.datasource.Converter;
import com.alibaba.csp.sentinel.datasource.FileRefreshableDataSource;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.sentinel.demo.common.FileUtil;
import com.sentinel.demo.common.SpringBeanUtils;
import com.sentinel.demo.pull.file.config.SentinelAspectConfiguration;
import lombok.Getter;

import java.util.List;

/**
 * 拉模式从数据源获取动态规则
 * @author dsy on 2019/12/3
 */
//public class AutoRefreshRule extends AutoRefreshDataSource {
//
//
//    public AutoRefreshRule(Converter configParser) {
//        super(configParser);
//    }
//
//    public AutoRefreshRule(Converter configParser, long recommendRefreshMs) {
//        super(configParser, recommendRefreshMs);
//    }
//
//    @Override
//    public Object readSource() throws Exception {
//        ClassLoader classLoader = getClass().getClassLoader();
//        String flowRulePath = FileUtil.getFile(SpringBeanUtils.getApplicationContext().getBean(SentinelAspectConfiguration.class).getPersistenceFilePath()).getAbsolutePath();//URLDecoder.decode(classLoader.getResource("FlowRule.json").getFile(), "UTF-8");
//
//        FileRefreshableDataSource<List<FlowRule>> flowRuleDataSource = new FileRefreshableDataSource<>(flowRulePath, flowRuleListParser);
//            FlowRuleManager.register2Property(flowRuleDataSource.getProperty());
//        return null;
//    }
//
//    @Getter
//    private  static Converter<String, List<FlowRule>> flowRuleListParser = source -> JSON.parseObject(source ,new TypeReference<List<FlowRule>>() {});
//}
